﻿// <copyright file="IdentityProviderException.cs" company="SharpSTS">
// Copyright (c) 2007, 2008 All Right Reserved, http://sharpsts.com/
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Barry Dorrans</author>
// <email>barryd@idunno.org</email>
// <date>2008-06-20</date>
// <summary>Defines the base class for Identity provider exceptions.</summary>

namespace SharpSTS
{
    using System;
    using System.Globalization;
    using System.ServiceModel;

    /// <summary>
    /// Defines the base class for Identity provider exceptions.
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage(
        "Microsoft.Design", 
        "CA1064:ExceptionsShouldBePublic",
        Justification = "In all cases that this will be caught within its own internal scope.")]
    [Serializable]
    internal abstract class IdentityProviderException : Exception
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="IdentityProviderException"/> class.
        /// </summary>
        /// <param name="faultCode">The fault code.</param>
        /// <param name="reasonText">The reason text.</param>
        protected IdentityProviderException(string faultCode, string reasonText) 
        {
            FaultCode = CreateFaultCode(faultCode);
            FaultReason = CreateFaultReason(reasonText);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="IdentityProviderException"/> class.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="info"/> parameter is null. </exception>
        /// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0). </exception>
        protected IdentityProviderException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context)
        {            
        }

        /// <summary>
        /// Gets the fault code.
        /// </summary>
        /// <value>The fault code.</value>
        public FaultCode FaultCode
        {
            get;
            protected set;
        }

        /// <summary>
        /// Gets the fault reason.
        /// </summary>
        /// <value>The fault reason.</value>
        public FaultReason FaultReason
        {
            get;
            protected set;
        }

        /// <summary>
        /// Creates the fault code for this exception.
        /// </summary>
        /// <param name="subCodeText">The sub code text.</param>
        /// <returns>A <see cref="FaultCode"/> with the specified subcode text.</returns>
        protected static FaultCode CreateFaultCode(string subCodeText)
        {
            FaultCode subCode = new FaultCode(subCodeText, Constants.WSIdentity.Namespace.Uri);
            return FaultCode.CreateSenderFaultCode(subCode);
        }

        /// <summary>
        /// Creates the fault reason.
        /// </summary>
        /// <param name="text">The text for the fault reason.</param>
        /// <returns>A <see cref="FaultReason"/> with the specified text.</returns>
        protected static FaultReason CreateFaultReason(string text)
        {
            FaultReasonText reasonText = new FaultReasonText(
                text,
                CultureInfo.CurrentCulture);

            return new FaultReason(reasonText);
        }
    }
}
